package com.easy.merchants.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.exception.CustomException;
import com.easy.merchants.mapper.PublicPraiseMapper;
import com.easy.merchants.pojo.PublicPraise;
import com.easy.merchants.service.IPublicPraiseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PublicPraiseServiceImpl extends ServiceImpl<PublicPraiseMapper, PublicPraise> implements IPublicPraiseService {
    private static final Logger log = LoggerFactory.getLogger(PublicPraiseServiceImpl.class);
    @Autowired
    private PublicPraiseMapper publicPraiseMapper;


    @Override
    public void updateEvaluate(PublicPraise publicPraise) {
        // 1.获取口碑信息
        LambdaQueryWrapper<PublicPraise> nowWrapper = new LambdaQueryWrapper<>();
        nowWrapper.eq(PublicPraise::getId, publicPraise.getId());
        PublicPraise one = publicPraiseMapper.selectOne(nowWrapper);

        // 2.获取修改后口碑评分
        double housingScore = getNowHousingScore(one, publicPraise.getHousing());
        double serviceScore = getNowServiceScore(one, publicPraise.getService());
        double evaluationScore = getNowEvaluationScore(one, publicPraise.getEvaluation());

        // 3.更新口碑信息
        LambdaUpdateWrapper<PublicPraise> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(PublicPraise::getId, publicPraise.getId());
        wrapper.set(PublicPraise::getHousing, housingScore);
        wrapper.set(PublicPraise::getService, serviceScore);
        wrapper.set(PublicPraise::getEvaluation, evaluationScore);
        wrapper.set(PublicPraise::getFrequency, one.getFrequency() + 1);
        publicPraiseMapper.update(null, wrapper);
    }

    @Override
    public List<Double> getEvaluate(Long id) {
        PublicPraise publicPraise = publicPraiseMapper.selectById(id);

        if (publicPraise == null) {
            log.error(" BB001{}", "获取口碑 用户不存在");
            throw new CustomException("用户不存在");
        }

        List<Double> scoreList = new ArrayList<>();
        scoreList.add(publicPraise.getGrade());
        scoreList.add(publicPraise.getHousing());
        scoreList.add(publicPraise.getService());
        scoreList.add(publicPraise.getEvaluation());
        return scoreList;
    }


    private double getNowHousingScore(PublicPraise one, double nowHousingScore) {

        double housing = one.getHousing();//房源得分
        int frequency = one.getFrequency();//次数

        double now = housing * frequency + nowHousingScore;//新房源总分

        return now / (frequency + 1);
    }

    private double getNowServiceScore(PublicPraise one, double nowServiceScore) {

        double serviceScore = one.getService();//服务得分
        int frequency = one.getFrequency();//次数

        double now = serviceScore * frequency + nowServiceScore;//服务总分

        return now / (frequency + 1);
    }

    private double getNowEvaluationScore(PublicPraise one, double nowEvaluationScore) {

        double evaluationScore = one.getEvaluation();//评价得分
        int frequency = one.getFrequency();//次数

        double now = evaluationScore * frequency + nowEvaluationScore;//评价总分

        return now / (frequency + 1);
    }
}
